import { createRouter,createWebHashHistory } from "vue-router";
import utils from "@/common/utils";


const routes=[
    {
        path:'/',
        redirect:'/login',

    },
    {
        path:'/login',
        name:'Login',
        component: ()=> import('@/components/Login'),
        meta:{
            title: "登录",
        }
    },
    {
        path:'/home',
        name:'Home',
        redirect: '/team',
        component: ()=> import('@/components/Home'),
        meta:{title: "首页"},
        children:[
            {
                path: '/team',
                name: 'Team',
                component: () => import('@/components/team/Team'),
            },
            {
                path: '/createTeam',
                name: 'CreateTeam',
                component: () => import('@/components/team/CreateTeam'),
                meta:{
                    title:'创建团队'
                }
            },
            {
                path: '/userDetail',
                name: 'UserDetail',
                component: () => import('@/components/user/UserDetail'),
                meta:{
                    title:'用户详情'
                }
            },
            {
                path: '/articleList',
                name: 'ArticleList',
                component: () => import('@/components/article/ArticleList'),
                meta:{
                    title:'文章管理'
                }
            },
            {
                path: '/resourcesList',
                name: 'ResourcesList',
                component: () => import('@/components/resources/ResourcesList'),
            },
            {
                path: '/statistics',
                name: 'Statistics',
                component: () => import('@/components/statistics/Statistics'),
            }
        ]
    }
    ]

const router = createRouter({
    history:createWebHashHistory(process.env.BASE_URL),
    routes
})

//全局前置路由守卫
router.beforeEach((to, from, next) => {
    const tokenStr = utils.localGetItem('token');
    let paths = to.path
    if (paths.includes('uc')) {
        if (!tokenStr) {
            alert('你还没有登录')
            return next('/login');
        }
    }
    if (to.path === '/login') return next();
    next()
});


router.afterEach((to,from)=>{
    document.title=to.meta.title
})

export default router
